package tr.gov.tubitak.bilgem.esya.certselector;

import tr.gov.tubitak.bilgem.esya.certviewer.viwerasn.ECertViewerCertificate;
import tr.gov.tubitak.uekae.esya.api.common.ESYAException;
import tr.gov.tubitak.uekae.esya.api.common.util.StringUtil;
import tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.BaseSmartCard;
import tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.SmartCardException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 14.11.2011
 * Time: 11:49
 * To change this template use File | Settings | File Templates.
 */
public class EBasicSmartCard {
    Long slotNo;
    String serialNumber;
    BaseSmartCard baseSmartCard;
    boolean isWorkWithQualified=false;

    ArrayList<ECertViewerCertificate> signatureCerts;

    public ArrayList<ECertViewerCertificate> getSignatureCerts() {
        if(signatureCerts == null)
        {
            signatureCerts = new ArrayList<ECertViewerCertificate>();
            try {
                 List<byte[]> signCerts =baseSmartCard.getSignatureCertificates();
                 for(int l=0;l<signCerts.size();++l)
                {
                    byte[] certBytes = signCerts.get(l);
                    ECertViewerCertificate cert = null;
                    try {
                        cert = new ECertViewerCertificate(certBytes);
                        if(isWorkWithQualified && !cert.isQualifiedCertficate())
                        {
                            continue;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    signatureCerts.add(cert);
                }
            } catch (SmartCardException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (ESYAException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        return signatureCerts;
    }

    public ArrayList<ECertViewerCertificate> getEncryptionCerts() {
        if(encryptionCerts == null)
        {
            encryptionCerts = new ArrayList<ECertViewerCertificate>();
            try {
                 List<byte[]> encCerts =baseSmartCard.getEncryptionCertificates();
                 for(int l=0;l<encCerts.size();++l)
                {
                    ECertViewerCertificate cert = new ECertViewerCertificate(encCerts.get(l));
                    encryptionCerts.add(cert);
                }
            } catch (SmartCardException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (ESYAException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        return encryptionCerts;
    }

    ArrayList<ECertViewerCertificate> encryptionCerts;

    public String getTerminal() {
        return terminal;
    }

    String terminal;

    public void setSlotNo(Long slotNo) {
        this.slotNo = slotNo;
    }

    public EBasicSmartCard(Long slotNo, String serialNumber, BaseSmartCard baseSmartCard, String terminal) {
        this.slotNo = slotNo;
        this.serialNumber = serialNumber;
        this.baseSmartCard = baseSmartCard;
        this.terminal = terminal;
    }



    public Long getSlotNo() {
        return slotNo;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public BaseSmartCard getBaseSmartCard() {
        return baseSmartCard;
    }

    boolean sessionOpened=false;
    void openSession()
    {
        if (!sessionOpened)
        {
            try {
                 baseSmartCard.openSession(slotNo);
                 serialNumber=StringUtil.toString(baseSmartCard.getSerial());
                sessionOpened = true;
            } catch (SmartCardException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}
